﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Abp.UI;
using ARchGL.Platform.Electrocar.Application.Dto;
using ARchGL.Platform.Electrocar.Core;
using ARchGL.Platform.Utils;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Abp.Linq.Extensions;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;

namespace ARchGL.Platform.Electrocar.Application
{
    /// <summary>
    /// 系统服务 实现
    /// </summary>
    public class SystemAppService : ApplicationService, ISystemAppService
    {
        private readonly IRepository<Accounts, Guid> accountsRepository;
        private readonly IRepository<Departments, Guid> departmentsRepository;

        public SystemAppService(IRepository<Accounts, Guid> _accountsRepository, IRepository<Departments, Guid> _departmentsRepository)
        {
            accountsRepository = _accountsRepository;
            departmentsRepository = _departmentsRepository;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="criteria">登录条件</param>
        /// <returns></returns>
        [Obsolete("登录使用 ABP 框架中的流程")]
        private async Task<AccountDto> Login(AccountsInput criteria)
        {
            var entity =await accountsRepository.FirstOrDefaultAsync(x => x.Telephone == "admin");
            return ObjectMapper.Map<AccountDto>(entity);
        }

        #region 成员相关



        #endregion

        #region 部门相关

        ///// <summary>
        ///// 新增部门
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<bool> CraeteDepartments(DepartmentsDto input)
        //{
        //    var entity = ObjectMapper.Map<Departments>(input);
        //    entity.Id = Guid.NewGuid();
        //    entity.CreationTime = DateTime.Now;
        //    await departmentsRepository.InsertAsync(entity);
        //    return true;
        //}

        ///// <summary>
        ///// 编辑部门
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<bool> ModifyDepartments(DepartmentsDto input)
        //{
        //    var entity = await departmentsRepository.GetAsync(input.Id);
        //    if (entity == null) throw new UserFriendlyException("部门不存在，请重新输入");
        //    if (!string.IsNullOrWhiteSpace(input.Name))
        //        entity.Name = input.Name;
        //    await departmentsRepository.UpdateAsync(entity);
        //    return true;
        //}

        ///// <summary>
        ///// 删除部门
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<bool> RemoveDepartments(DepartmentsDto input)
        //{
        //    var entity = await departmentsRepository.GetAsync(input.Id);
        //    if (entity == null) throw new UserFriendlyException("部门不存在，请重新输入");

        //    //部门下存在成员不能删除

        //    await departmentsRepository.DeleteAsync(entity);
        //    return true;
        //}

        ///// <summary>
        ///// 获取部门-分页数据集合
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<PagedResultDto<DepartmentsDto>> QueryDepartmentsPagedList(DepartmentsInput input)
        //{
        //    var query = departmentsRepository.GetAll()
        //        .WhereIf(!string.IsNullOrWhiteSpace(input.Keywords), x => x.Name.Contains(input.Keywords) || x.Description.Contains(input.Keywords))
        //        .WhereIf(input.ParentId.HasValue, x => x.ParentId == input.ParentId);

        //    var result = await query.OrderByDescending(x => x.CreationTime)
        //        .ToPageResultAsync(input);

        //    return new PagedResultDto<DepartmentsDto>(query.Count(),
        //        ObjectMapper.Map<List<DepartmentsDto>>(query.ToList()));
        //}

        #endregion

        ///// <summary>
        ///// 获取部门-分页数据集合
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<PagedResultDto<DepartmentsDto>> QueryDepartmentsPagedList(DepartmentsInput input)
        //{
        //    var query = departmentsRepository.GetAll();
        //    var totalCount = await query.CountAsync();

        //    var result = await query.ToPageResultAsync(input.SkipCount, input.MaxResultCount);
        //    //result.Items = ObjectMapper.Map<List<DepartmentsDto>>(await query.ToListAsync());
        //    //return Task.FromResult(result);
        //    return new PagedResultDto<DepartmentsDto>(totalCount,
        //        ObjectMapper.Map<List<DepartmentsDto>>(await query.ToListAsync()));
        //}
    }
}
